package com.awspaas.user.apps.processforsap.impl.send;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.exception.BPMNError;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.processforsap.model.MessageHeader;
import com.awspaas.user.apps.processforsap.util.HttpClientUtil;
import com.awspaas.user.apps.processforsap.util.ParentInterface;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class SenderSD003Impl extends ParentInterface {

    public SenderSD003Impl(JSONObject jsonObject) {
        super(jsonObject);
    }

    @Override
    public void dueInterFaceData() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String result = "";
        String resultType = "S";
        MessageHeader messageHeader = super.messageHeader;
        JSONObject sendData = super.sendData;
        String bindid = sendData.getString("BINDID");
        BO boMain = SDK.getBOAPI().query("BO_EU_SAPSD_KHSWXGXYPS").detailByBindId(bindid);
        JSONObject newJsonResult = new JSONObject();
        List<JSONObject> newList = new ArrayList<>();

        /**
         * 获取币种
         */
        String field0017 = boMain.getString("FIELD0017");
        List<BO> list = SDK.getBOAPI().query("BO_EU_SAP_SWLIST").bindId(bindid).list();
        try {

            JSONArray Requests = new JSONArray();
            for (BO bo : list) {
                String yxqyzt = bo.getString("YXQYZT");
                String[] yss = yxqyzt.split(",");

                for (String ys : yss) {

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("BPMID", boMain.getBindId());
                    jsonObject.put("AUART", "Z201");
                    jsonObject.put("VKORG", ys);
                    jsonObject.put("VTWEG", "00");
                    //根据区域关联
                    String field0014 = boMain.getString("FIELD0014");
                    if (field0014.equals("华东一部")) {
                        jsonObject.put("SPART", "10");
                    } else if (field0014.equals("华南部") || field0014.equals("华北部")) {
                        jsonObject.put("SPART", "20");
                    } else if (field0014.equals("台湾部") || field0014.equals("华东二部")) {
                        jsonObject.put("SPART", "30");
                    } else if (field0014.equals("欧美部")) {
                        jsonObject.put("SPART", "50");
                    }
                    jsonObject.put("KUNNR1", bo.getString("CUSTCODE"));
                    jsonObject.put("KUNNR2", bo.getString("CUSTCODE"));
                    jsonObject.put("BSTNK", boMain.getString("BSTNK"));

                    //事件处理代码
                    String val = boMain.getString("FIELD0017");
                    String val1 = boMain.getString("FIELD0036");
                    String val2 = boMain.getString("FIELD0038");
                    if (val.equals("RMB")) {
                        if (val1.equals("甬矽承担") && val2.equals("甬矽承担")) {
                            jsonObject.put("INCO1_CK","Z1");
                        }
                        if (val1.equals("客户承担") && val2.equals("客户承担")) {
                            jsonObject.put("INCO1_CK","Z2");
                        }
                        if (val1.equals("甬矽承担") && val2.equals("客户承担")) {
                            jsonObject.put("INCO1_CK","Z4");
                        }
                        if (val1.equals("客户承担") && val2.equals("甬矽承担")) {
                            jsonObject.put("INCO1_CK","Z3");
                        }
                    }else{
                        jsonObject.put("INCO1_CK", boMain.getString("FIELD0012"));
                    }
                    jsonObject.put("INCO1_JK", boMain.getString("FIELD0065"));
                    jsonObject.put("VBEGDAT", boMain.getString("FIELD0030").replace("-", ""));
                    jsonObject.put("VENDDAT", boMain.getString("FIELD0031").replace("-", ""));
                    jsonObject.put("AUDAT", SDK.getRuleAPI().executeAtScript("@date").replace("-", ""));
                    //结算方式：入甬矽库结算|出甬矽库结算|按终端结算（入甬矽库结算）|按终端结算（出甬矽库结算）|按型号结算
                    if ("入甬矽库结算".equals(boMain.getString("FIELD0008")) || "按终端结算（入甬矽库结算）".equals(boMain.getString("FIELD0008"))) {
                        jsonObject.put("VSBED", "01");
                    } else {
                        jsonObject.put("VSBED", "02");
                    }
                    jsonObject.put("ZTERM", boMain.getString("FIELD0018"));
                    jsonObject.put("INCO1_JK", boMain.getString("FIELD0065"));
                    jsonObject.put("ZSFBXQ", boMain.getString("ZSFBXQ"));
                    jsonObject.put("ZSFYHGTK", boMain.getString("FIELD0090"));
                    jsonObject.put("ZSFYSRQRTK", boMain.getString("ZSFYSRQRTK"));
                    jsonObject.put("ZSFYHT", boMain.getString("ZSFYHT"));
                    jsonObject.put("ZNDBXCDF", boMain.getString("FIELD0036"));
                    jsonObject.put("ZNDYFCDF", boMain.getString("FIELD0038"));
                    jsonObject.put("ZWDBXCDF", boMain.getString("FIELD0037"));
                    jsonObject.put("ZWDYFCDF", boMain.getString("FIELD0039"));
                    Requests.add(jsonObject);
                }
                sendData.put("Requests", Requests);
                BO configBo = messageHeader.getConfigBo();
                String url = configBo.getString("SEND_URL");
                result = HttpClientUtil.doPost(url, sendData.toJSONString(), SDK.getRuleAPI().executeAtScript("@getSapParamas(PONAME)"), SDK.getRuleAPI().executeAtScript("@getSapParamas(POPASSWORD)"));
                JSONObject jsonObjectR = JSONObject.parseObject(result);
                JSONArray jsonArray = jsonObjectR.getJSONArray("Responses");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1R = jsonArray.getJSONObject(i);
                    String msgtype = jsonObject1R.getString("MSGTYPE");
                    if (msgtype.equals("E")) {
                        resultType = "E";
                    } else {
                        SDK.getProcessAPI().setVariable(bindid, messageHeader.getInterface_ID(), true);
                    }
                    newList.add(jsonObject1R);
                }
            }
            //封装最终数据
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("Type", resultType);
            jsonObject1.put("MESSAGE", newList);
            newJsonResult.put("Responses", jsonObject1);
            result = JSONObject.toJSONString(newJsonResult);
        } catch (Exception e) {
            e.printStackTrace();
            super.resultType = false;
            throw new BPMNError("0331", e.getMessage());
        } finally {
            super.resultSendData = sendData.toJSONString();
            super.returnData = result;
        }

    }
}